/*
 *  Copyright 2015 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos;

import com.google.blockly.ohos.control.BlocklyController;
import com.google.blockly.ohos.ui.BlockView;
import com.google.blockly.ohos.ui.VirtualWorkspaceView;
import com.google.blockly.ohos.ui.WorkspaceView;
import com.google.blockly.model.Workspace;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

/**
 * The {@code WorkspaceFragement} holds the active {@link WorkspaceView} and workspace
 * {@link BlockView}s.
 * <p/>
 * The workspace can be configured as scrollable or fixed via the <code>scrollable</code> attribute.
 */
public class WorkspaceFraction extends Fraction {
    private static final String TAG = "WorkspaceFraction";

    private BlocklyController mController;
    private Workspace mWorkspace;
    private VirtualWorkspaceView mVirtualWorkspaceView;
    private WorkspaceView mWorkspaceView;

    private boolean mDrawGrid = true;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component rootView = scatter.parse(ResourceTable.Layout_fraction_workspace, container, false);

        mVirtualWorkspaceView =
                (VirtualWorkspaceView) rootView.findComponentById(ResourceTable.Id_virtual_workspace);
        mWorkspaceView = (WorkspaceView) rootView.findComponentById(ResourceTable.Id_workspace);
        mVirtualWorkspaceView.setWorkspaceView(mWorkspaceView);

        if (mController != null) {
            mVirtualWorkspaceView.setZoomBehavior(
                    mController.getWorkspaceHelper().getZoomBehavior());
        }

        return rootView;
    }

    /**
     * Sets the controller to use in this Fraction for instantiating views. This should be the same
     * controller used for any associated {@link FlyoutFraction FlyoutFractions}.
     *
     * @param controller The controller backing this Fraction.
     */
    public void setController(BlocklyController controller) {
        if (controller == null || controller == mController) {
            return; // no-op
        }

        mController = controller;
        mWorkspace = mController.getWorkspace();

        mController.initWorkspaceView(mWorkspaceView);

        if (mVirtualWorkspaceView != null) {
            mVirtualWorkspaceView.setZoomBehavior(
                    mController.getWorkspaceHelper().getZoomBehavior());
        }
    }

    /**
     * @return The workspace being used by this Fraction.
     */
    public Workspace getWorkspace() {
        return mWorkspace;
    }

    /**
     * @return The {@link WorkspaceView} inside this Fraction.
     */
    public WorkspaceView getWorkspaceView() {
        return mWorkspaceView;
    }
}
